package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.YgtVideoAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.LifeVideoListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.PermissionUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PromptPopupWindow;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class YgtVideoActivity extends BaseActivity {

    String classifyId;

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    PromptPopupWindow promptPopupWindow;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fancy_video, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("视频 ");
        classifyId = getIntent().getStringExtra("classifyId");
        setRight("添加视频", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PermissionUtils.cameraIsCanUse()) {

                    Intent intent = new Intent(YgtVideoActivity.this, VideoRecorderActivity.class);
                    intent.putExtra(ReleaseVideoActivity.FLAG_KEY, classifyId);
                    startActivity(intent);

//                    YgtVideoActivity.this.startActivity(new Intent(YgtVideoActivity.this, VideoRecorderActivity.class));
                } else {
                    PermissionUtils.openSettingActivity(YgtVideoActivity.this, "没有相机和录音权限，无法开启这个功能，请开启权限");
                }
            }
        });

        classifyId = getIntent().getStringExtra("classifyId");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);


        ArrayList itemDatas = new ArrayList<>();

        fancyVideoAdapter = new YgtVideoAdapter(this, itemDatas);
        fancyVideoAdapter.setDeleteCallBack(new YgtVideoAdapter.DeleteCallBack() {
            @Override
            public void deleteCallBack(int p, final String item) {
                position = p;
                videoId = item;
                if (promptPopupWindow == null) {
                    promptPopupWindow = new PromptPopupWindow(YgtVideoActivity.this, toolbar);
                    promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                        @Override
                        public void confirm() {

                            if (fancyVideoAdapter.itemDatas.get(position).getIs_system().equals("1")) {
                            } else {
                                sygtDelVideo();
                            }

                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }

                if (fancyVideoAdapter.itemDatas.get(position).getIs_system().equals("1")) {
                    promptPopupWindow.setPromptTitle("系统视频不能删除");
                } else {
                    promptPopupWindow.setPromptTitle("是否确定删除视频");
                }

                promptPopupWindow.showPopupWindow();
            }
        });

        recycleView.setAdapter(fancyVideoAdapter);


        fancyVideoAdapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getData(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
                getData(false);
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData(true);
    }

    YgtVideoAdapter fancyVideoAdapter;

    int page = 0;

    private void getData(final boolean isRefresh) {

        Map maps = new HashMap();


        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        maps.put("page", page + "");

        maps.put("classifyId", classifyId);

        HttpMehtod.getInstance().ygtVideoList(maps, new IdeaObserver<BaseDataBean<List<LifeVideoListDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<LifeVideoListDataBean>> listBaseDataBean) {

                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        return;
                    }

                    if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }

                    fancyVideoAdapter.notifyDataSetChanged((ArrayList<LifeVideoListDataBean>) listBaseDataBean.getData());
                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    fancyVideoAdapter.addAll(listBaseDataBean.getData());
                }
            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }

        });

    }

    private int position = 0;
    private String videoId = "";

    private void sygtDelVideo() {
        Map map = new HashMap();


        map.put("videoId", videoId + "");
        map.put("classifyId", classifyId + "");


        HttpMehtod.getInstance().sygtDelVideo(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {

                ToastUtil.show("删除成功");
                fancyVideoAdapter.itemDatas.remove(position);
                fancyVideoAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(int errorCode) {
            }
        });
    }

}
